package work;

import com.alibaba.fastjson.JSON;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import utils.DateUtils;
import utils.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SchedulingTableMain {

    public static void main(String[] args) {
        ScheduleOnlineOfficeVO scheduleOnlineOfficeVO = new ScheduleOnlineOfficeVO();
        scheduleOnlineOfficeVO.setLoginName("lilin2");
        scheduleOnlineOfficeVO.setMonth("201911");
        scheduleOnlineOfficeVO.setPassword("123456");
        scheduleOnlineOfficeVO.setOperationType("postman_submit");
        scheduleOnlineOfficeVO.setSchedulingTableList(getSchedulingTableList(11));

        System.out.println(JSON.toJSONString(scheduleOnlineOfficeVO));
    }

    public static List<SchedulingTable> getSchedulingTableList(int month){
        String filePath = "/Users/lining/Desktop/每月排班/20191101自动排班.xls";
        int days = 30;
        int groupNameIndex = 0;//组名下标
        int groupIdIndex = 1;//组id下标
        int statusOffset = 0;//表格偏移

        List<SchedulingTable> schedulingTableList = new ArrayList<>();
        File file = new File(filePath);
        try {
            Workbook wb = Workbook.getWorkbook(file);
            Sheet sheet = wb.getSheet(0);
            int rows = sheet.getRows();
            for(int i=1;i<rows;i++){
                Cell[] row = sheet.getRow(i);

                SchedulingTable itemTable = new SchedulingTable();
                itemTable.setGroupId(row[groupIdIndex].getContents());
                itemTable.setGroupName(row[groupNameIndex].getContents());

                List<ScheduleDayData> schedulingTable = new ArrayList();
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.MONTH,month - 1);
                for(int d = 1;d<=days ;d++){
                    calendar.set(Calendar.DAY_OF_MONTH,d);
                    String status = row[groupIdIndex + d + statusOffset].getContents();
                    if(StringUtils.isNullOrEmpty(status))continue;
                    ScheduleDayData dayData = new ScheduleDayData();
                    dayData.setDate(DateUtils.formatDate("yyyy-MM-dd 00:00:00",calendar.getTime()));
                    dayData.setStatus(status);
                    schedulingTable.add(dayData);
                }
                itemTable.setSchedulingTable(schedulingTable);
                schedulingTableList.add(itemTable);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return schedulingTableList;
    }
}
